package cn.hs.controller;/**
 * @author Jovan Hou 2024/10/23
 **/

import cn.hs.entity.Article;
import cn.hs.entity.Articletype;
import cn.hs.entity.Fm;
import cn.hs.service.ArticleTypeService;
import cn.hs.service.FmService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ArticleTypeController
 * @Description TODO
 * @Author CnYuu
 * @Date 2024年10月23日 19:38
 * @Version 1.0
 */
@RestController
@RequestMapping("/articletype")
public class ArticleTypeController
{

    @Autowired
    private ArticleTypeService service;

    @GetMapping("/selectAll")
    public Result selectAll(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @Param("id")
            Integer id,
            @Param("name")
            String name
    ){
        Articletype articletype = new Articletype(id,name);
        PageInfo serviceAll = service.findAll(pageNum, pageSize, articletype);
        // 用户不存在
        return Result.success(serviceAll);
    }




    @PostMapping("/insert")
    public Result insert(
            @RequestBody
            Articletype fm
    ){
        int insert = service.insert(fm);
        return Result.success(insert);
    }


  @GetMapping("/deleteAllByTypeId")
  public Result deleteAll(
          @Param("id") Integer id
  ) {
      try {
          int i = service.deleteById(id);
          if (i > 0) {
              return Result.success(i);
          } else {
              return Result.error("删除失败，数据不存在");
          }
      } catch (DataIntegrityViolationException e) {
          // 捕获外键约束异常
          return Result.error("无法删除，该类型下存在关联文章");
      } catch (Exception e) {
          // 处理其他可能的异常
          return Result.error("系统异常，请稍后重试");
      }
  }
    @GetMapping("/delete")
    public Result delete(
            @Param("id") Integer id
    ) {
        try {
            int i = service.deleteById(id);
            if (i > 0) {
                return Result.success(i);
            } else {
                return Result.error("删除失败，数据不存在");
            }
        } catch (DataIntegrityViolationException e) {
            // 捕获外键约束异常
            return Result.error("无法删除，该类型下存在关联文章");
        } catch (Exception e) {
            // 处理其他可能的异常
            return Result.error("系统异常，请稍后重试");
        }
    }



    @PutMapping("/update")
    public Result update(
            @RequestBody Articletype fm
    ){
        int update = service.update(fm);
        return Result.success(update);
    }

    @GetMapping("/selectByForId")
    public Result selectByForId(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
          @Param("id")
          Integer id
    ){
        PageInfo info = service.findbyForignId(pageNum, pageSize, id);
        return Result.success(info);
    }
}
